package com.qk.management.controller;

import com.qk.common.PageResult;
import com.qk.common.Result;
import com.qk.common.constant.ClueStatusConstants;
import com.qk.domain.clue.ClueDO;
import com.qk.dto.clue.ClueDTO;
import com.qk.dto.clue.ClueQueryDto;
import com.qk.dto.clue.ClueTrackRecordDTO;
import com.qk.dto.clue.UpdateClueInfoDTO;
import com.qk.entity.Clue;
import com.qk.management.aop.anno.LogAnno;
import com.qk.management.service.ClueService;
import com.qk.vo.clue.ClueVO;
import com.qk.vo.clue.GetClueTrackRecordVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

/**
 * @author kaoM1Jy
 * @date 2025/10/13 20:13
 * @description:
 */
@Slf4j
@RestController
@RequestMapping("/clues")
@SuppressWarnings("all")
public class ClueController {
    @Autowired
    private ClueService clueService;


    @GetMapping("/pool")
    public Result getClueTrackRecord(ClueTrackRecordDTO dto) {
        log.info("查询线索池");
        PageResult<GetClueTrackRecordVO> result = clueService.getClueTrackRecord(dto);
        return Result.success(result);
    }

    @LogAnno
    @PutMapping("/false/{id}")
    public void falseClue(@PathVariable Integer id) {
        log.info("将线索转为无效, id: {}", id);
        clueService.updateById(Clue.builder()
                .id(id)
                .status(ClueStatusConstants.FAKE_CLUE)
                .updateTime(LocalDateTime.now())
                .build());
    }
    @LogAnno
    @PutMapping("/toBusiness/{id}")
    public void toBusiness(@PathVariable Integer id) {
        log.info("将线索转为商机, id: {}", id);
        clueService.toBusiness(id);
    }

    @LogAnno
    @PutMapping
    public Result updateClueInfoById(@RequestBody UpdateClueInfoDTO dto){
        log.info("=========================拦截器处理线程名称是：{}",Thread.currentThread().getName());
        clueService.updateClueInfoById(dto);
        return Result.success();
    }




    @GetMapping("/{id}")
    public Result getClueById(@PathVariable Integer id) {
        ClueVO vo = clueService.getClueInfoById(id);
        return Result.success(vo);
    }
    @LogAnno
    @PutMapping("/assign/{clueId}/{userId}")
    public Result updateClueById(@PathVariable Integer clueId, @PathVariable Integer userId) {
        clueService.updateById(Clue.builder()
                .id(clueId)
                .userId(userId)
                .updateTime(LocalDateTime.now())
                .status(ClueStatusConstants.WAIT_FOLLOW_UP)
                .build());
        return Result.success();
    }

    @GetMapping
    public Result getPage(ClueQueryDto dto){
        PageResult<ClueDO> result = clueService.clueList(dto);
        return Result.success(result);
    }

    @LogAnno
    @PostMapping
    public Result addClue(@RequestBody ClueDTO dto) {
        clueService.addClue(dto);
        return Result.success();
    }


}
